package com.yuanlrc.base.controller.home;

import com.yuanlrc.base.bean.CodeMsg;
import com.yuanlrc.base.bean.Result;
import com.yuanlrc.base.constant.SessionConstant;
import com.yuanlrc.base.entity.common.Goods;
import com.yuanlrc.base.entity.common.HomeUser;
import com.yuanlrc.base.service.common.GoodsCategoryService;
import com.yuanlrc.base.service.common.GoodsService;
import com.yuanlrc.base.service.common.HomeUserService;
import com.yuanlrc.base.util.SessionUtil;
import com.yuanlrc.base.util.ValidateEntityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

/**
 * 账号中心控制器
 */
@RequestMapping("/home/homeuser")
@Controller
public class homeUserController {
    @Autowired
    private HomeUserService homeUserService;
    @Autowired
    private GoodsCategoryService goodsCategoryService;
    @Autowired
    private GoodsService goodsService;

    @GetMapping("/index")
    public String index(Model model){
        HomeUser loginHomeUser = (HomeUser) SessionUtil.get(SessionConstant.SESSION_HOMEUSER_LOGIN_KEY);
        model.addAttribute("goodsList",goodsService.findByHomeUser(loginHomeUser));
        return "/home/homeuser/index";
    }

    /**
     * 用户信息修改
     * @param homeUser
     * @return
     */
    @PostMapping("/edit_info")
    @ResponseBody
    public Result<Boolean> editInfo(HomeUser homeUser){
        HomeUser loginHomeUser = (HomeUser) SessionUtil.get(SessionConstant.SESSION_HOMEUSER_LOGIN_KEY);
        loginHomeUser.setQq(homeUser.getQq());
        loginHomeUser.setAddress(homeUser.getAddress());
        loginHomeUser.setMobile(homeUser.getMobile());
        loginHomeUser.setNickname(homeUser.getNickname());
        if(homeUserService.save(loginHomeUser)==null){
            return Result.error(CodeMsg.ADMIN_HOMEUSER_EDITINFO_ERROR);
        }
        SessionUtil.set(SessionConstant.SESSION_HOMEUSER_LOGIN_KEY,loginHomeUser);
        return Result.success(true);
    }

    /**
     * 用户头像修改
     * @param useridPic
     * @return
     */
    @PostMapping("/update_head_pic")
    @ResponseBody
    public Result<Boolean> updateHeadPic(@RequestParam(name="useridPic") String useridPic){
        HomeUser loginHomeUser = (HomeUser) SessionUtil.get(SessionConstant.SESSION_HOMEUSER_LOGIN_KEY);
        loginHomeUser.setUseridPic(useridPic);
        if(homeUserService.save(loginHomeUser)==null){
            return Result.error(CodeMsg.ADMIN_HOMEUSER_EDITINFO_ERROR);
        }
        SessionUtil.set(SessionConstant.SESSION_HOMEUSER_LOGIN_KEY,loginHomeUser);
        return Result.success(true);
    }

    /**
     * 商品发布页面
     * @param model
     * @return
     */
    @GetMapping("/publish")
    public String publish(Model model){
        return "home/homeuser/publish";
    }

    /**
     * 商品物品表单提交
     * @param goods
     * @return
     */
    @PostMapping("/publish")
    @ResponseBody
    public Result<Boolean> publish(Goods goods){
        CodeMsg validate = ValidateEntityUtil.validate(goods);
        if(validate.getCode()!= CodeMsg.SUCCESS.getCode()){
            return Result.error(validate);
        }
        if(goods.getGoodsCategory()==null||goods.getGoodsCategory().getId()==null||goods.getGoodsCategory().getId().longValue()==-1){
            return Result.error(CodeMsg.HOME_HOMEUSER_PUBLISH_COTEGORY_EMPTY);
        }
        HomeUser loginHomeUser = (HomeUser) SessionUtil.get(SessionConstant.SESSION_HOMEUSER_LOGIN_KEY);
        goods.setHomeUser(loginHomeUser);
        if(goodsService.save(goods)==null){
            return Result.error(CodeMsg.HOME_HOMEUSER_PUBLISH_ERROR);
        }
        return Result.success(true);
    }

    /**
     *物品编辑页面
     * @param id
     * @param model
     * @return
     */
    @GetMapping("/edit_goods")
    public String edit_goods(@RequestParam(name = "id") Long id,Model model){
        HomeUser loginHomeUser= (HomeUser)SessionUtil.get(SessionConstant.SESSION_HOMEUSER_LOGIN_KEY);
        Goods goods = goodsService.finds(id, loginHomeUser.getId());
        if(goods==null){
            model.addAttribute("msg","物品不存在");
            return "error/runtime_error";
        }
        model.addAttribute("goods",goods);
        return "home/homeuser/edit_goods";
    }
}
